import Vue from 'vue'
import VueRouter from 'vue-router'

import Layout from '@/components/Layout'
import Login from '@/views/login'
import Home from '@/views/home'
// platform
import PlatformManage from '@/views/platform/platformManage'
// user
import UserManage from '@/views/user/userManage'
import UserRoleManage from '@/views/user/userRoleManage'
import RoleApplicationManage from '@/views/user/roleApplicationManage'
import RoleApplicationResourcesManage from '@/views/user/roleApplicationResourcesManage'
import RoleResources from '@/views/user/roleResources'
// temporary
import UserTemporaryApplicationManage from '@/views/temporary/userTemporaryApplicationManage'
import UserTemporaryApplicationResourcesManage from '@/views/temporary/userTemporaryApplicationResourcesManage'
import UserTemporaryResources from '@/views/temporary/userTemporaryResources'
// application
import ApplicationManage from '@/views/application/applicationManage'
import ApplicationResourcesManage from '@/views/application/applicationResourcesManage'
// resources
import AuthorityManage from '@/views/authority/authorityManage'



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: '/home',
      component: Home,
      meta: {
        title: '首页',
        requireAuth: true
      }
    }]
  },

  // platform
  {
    path: '/platformManage',
    component: Layout,
    children: [{
      path: '/',
      component: PlatformManage,
      meta: {
        title: '平台管理',
        requireAuth: true
      }
    }]
  },
  // user
  {
    path: '/userManage',
    component: Layout,
    children: [{
      path: '/',
      component: UserManage,
      meta: {
        title: '用户管理',
        requireAuth: true
      }
    }]
  },
  {
    path: '/userRoleManage',
    component: Layout,
    children: [{
      path: '/',
      component: UserRoleManage,
      meta: {
        title: '用户角色管理',
        requireAuth: true
      }
    }]
  },
  {
    path: '/roleApplicationManage',
    component: Layout,
    children: [{
      path: '/',
      component: RoleApplicationManage,
      meta: {
        title: '角色应用管理',
        requireAuth: true
      }
    }]
  },
  {
    path: '/roleApplicationResourcesManage',
    component: Layout,
    children: [{
      path: '/',
      component: RoleApplicationResourcesManage,
      meta: {
        title: '角色应用资源管理',
        requireAuth: true
      }
    }]
  },
  {
    path: '/roleResources',
    component: Layout,
    children: [{
      path: '/',
      component: RoleResources,
      meta: {
        title: '角色权限管理',
        requireAuth: true
      }
    }]
  },
  // temporary
  {
    path: '/userTemporaryApplicationManage',
    component: Layout,
    children: [{
      path: '/',
      component: UserTemporaryApplicationManage,
      meta: {
        title: '用户临时应用管理',
        requireAuth: true
      }
    }]
  },
  {
    path: '/userTemporaryApplicationResourcesManage',
    component: Layout,
    children: [{
      path: '/',
      component: UserTemporaryApplicationResourcesManage,
      meta: {
        title: '用户临时应用资源管理',
        requireAuth: true
      }
    }]
  },
  {
    path: '/userTemporaryResources',
    component: Layout,
    children: [{
      path: '/',
      component: UserTemporaryResources,
      meta: {
        title: '用户临时权限管理',
        requireAuth: true
      }
    }]
  },
  // application
  {
    path: '/applicationManage',
    component: Layout,
    children: [{
      path: '/',
      component: ApplicationManage,
      meta: {
        title: '应用管理',
        requireAuth: true
      }
    }]
  }, 
  {
    path: '/applicationResourcesManage',
    component: Layout,
    children: [{
      path: '/',
      component: ApplicationResourcesManage,
      meta: {
        title: '应用资源管理',
        requireAuth: true
      }
    }]
  },
  // authority
  {
    path: '/authorityManage',
    component: Layout,
    children: [{
      path: '/',
      component: AuthorityManage,
      meta: {
        title: '权限管理',
        requireAuth: true
      }
    }]
  },
  // login
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '登录',
      requireAuth: false
    }
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

import {getUserInfo} from '@/api/login'
/**
 * 路由拦截器
 * Vue Router 中的钩子函数 beforeEach((to, from, next)
 * 当进行路由跳转前，进行判断是否已登录过，等录则正常访问，否则返回登录页
 * 
 * to: 即将要进入的目标路由对象
 * from: 即将要离开的路由目标对象
 * next: 一个方法，可以指定路由地址，进行路由跳转
 */
router.beforeEach((to, from, next) =>   {
    // 1. 判断该路由是否需要登录权限
    if (to.meta.requireAuth) {
        // 1.1 需要登录，判断本地是否存在token
        if (localStorage.getItem("bopu-upms-token")) {
            // 1.1.1 有token，判断本地是否有用户信息
            if (localStorage.getItem("bopu-upms-user")) {
                // 1.1.1.1 存在用户信息，正常路由目标
            } else {
                // 1.1.1.2 不存在用户信息，通过token获取用户信息
                getUserInfo(token).then(respones => {
                    const resp = respones.data
                    if (resp.flag) {
                        // 获取用户信息成功，保存用户信息并跳转目标路由
                        localStorage.setItem('bopu-upms-token',resp.data.token);
                        localStorage.setItem('bopu-upms-user',JSON.stringify(respUser.data));
                    } else {
                        // 获取用户信息失败，跳转登录页
                        next({path: '/login'})
                    }
                })
            }

            next()
        } else {
            // 1.1.2 没有token,跳转到登陆页面
            next({path: '/login'})
        }
    } else {
        // 1.2 不需要登录权限页面直接跳转
        next();
    }
});

export default router
